package cn.edu.abtu.controller;

import cn.edu.abtu.dto.RolePermissionDTO;
import cn.edu.abtu.entity.SysRole;
import cn.edu.abtu.service.SysPermissionService;
import cn.edu.abtu.service.SysRoleService;
import cn.edu.abtu.utils.AjaxResult;
import cn.edu.abtu.vo.RolePermissionVo;
import cn.edu.abtu.vo.query.RoleQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/api/sysRole")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    @Resource
    private SysPermissionService sysPermissionService;

    @PostMapping("/saveRoleAssign")
    public AjaxResult saveRoleAssign(@RequestBody RolePermissionDTO rolePermissionDTO){
        boolean result = sysRoleService.saveRolePermission(rolePermissionDTO.getRoleId(), rolePermissionDTO.getList());
        if (result){
            return AjaxResult.success("分配权限成功!",null);
        }
        return AjaxResult.error("分配权限失败",null);
    }

    // 根据用户id和角色id查询所对应的菜单信息
    @GetMapping("/getAssignPermissionTree")
    public AjaxResult getAssignPermissionTree(Long userId, Long roleId){
        // 调用查询权限树的方法
        RolePermissionVo permissionTree = sysPermissionService.getPermissionTree(userId, roleId);
        return AjaxResult.success("查询成功! ",permissionTree);
    }

    @GetMapping("/page")
    public AjaxResult page(RoleQueryVo roleQueryVo){
        return sysRoleService.page(roleQueryVo);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysRole sysRole){
        boolean save = sysRoleService.save(sysRole);
        if (save){
            return AjaxResult.success("添加角色信息成功!",null);
        }
        return AjaxResult.error("添加角色信息失败",null);
    }

    @PutMapping("/update")
    public AjaxResult update(@RequestBody SysRole sysRole){
        boolean update = sysRoleService.updateById(sysRole);
        if (update){
            return AjaxResult.success("修改角色信息成功!",null);
        }
        return AjaxResult.error("修改角色信息失败",null);
    }

    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id){
        return sysRoleService.removeById(id);
    }

}

